package com.kcwl.idea.plugins.gateway.handler;

import cn.hutool.core.collection.CollUtil;
import com.kcwl.idea.plugins.base.checkboxtree.CheckBoxNodeData;

import javax.swing.tree.DefaultMutableTreeNode;
import java.util.List;

public class ResourceTreeUtil {

    public static DefaultMutableTreeNode treeCheckBoxTreeNode(DefaultMutableTreeNode parent, Menu menu, List<String> hasResources) {
        DefaultMutableTreeNode root = addTreeNode(parent, menu ,menu.isChecked());
        List<Menu> children = menu.getChildren();
        if (CollUtil.isEmpty(children)) {
            return root;
        }
        for (Menu child : children) {
            child.setProductId(menu.getProductId());
            if (CollUtil.isNotEmpty(hasResources)) {
                child.setChecked(hasResources.contains(child.getId()));
            }
            DefaultMutableTreeNode checkBoxTreeNode = treeCheckBoxTreeNode(root, child, hasResources);
            root.add(checkBoxTreeNode);
        }
        return root;
    }

    public static DefaultMutableTreeNode addTreeNode(
            final DefaultMutableTreeNode parent, final Menu menu,
            final boolean checked) {
        final CheckBoxNodeData data = new CheckBoxNodeData(menu, checked);
        final DefaultMutableTreeNode node = new DefaultMutableTreeNode(data);
        parent.add(node);
        return node;
    }
}
